<?php

// n_view.php
// Michael Baker
// 2010.07.14
//
// View node in detail

session_start();

define("IN_MDM", true);
define("CUR_PAGE_TITLE", "Node detail");
define("CUR_FLAG", "n.view");

include '../includes/all.php';
include 'inc/header.php';

if ( !( array_key_exists( "id", $_GET ) && is_numeric( $_GET["id"] ) ) ) {
  echo ui_error( "No node ID supplied" );
  include 'inc/footer.php';
  die;
}

$id = $_GET["id"];

try {
  $n = $dbase->get_node($id);
}
catch (DatabaseException $e) {
  echo ui_error( $e->getMessage() );
  include 'inc/footer.php';
  die;
}

$_SESSION[ "from" ] = "n_view.php?id=$id";
$stitle = $n->get_title(0);
$ltitle = $n->get_title(1);
$addr   = $n->get_address();
$suff   = ($n->get_has_db()) ? "MA" : "MP";

$nc_id_arr = $dbase->search_for_nodecouples($id);

echo "<div class=\"nodemap\"><h2>Node relationships</h2><p style=\"text-align: center\"><img src=\"get_net.php?id=$id\" /></p></div>";

?>

<table class="type2p" cellspacing="1">
  <caption><?php echo "#$id: $ltitle"; ?></caption>
  <col class="col1" /><col class="col2" />
  
  <thead>
    <tr>
      <th>Property</th>
      <th>Value</th>
    </tr>
  </thead>
  
  <tbody>
    <tr>
      <td>Node Title:</td>
      <td><?php echo $ltitle . "<br />" . $stitle; ?></td>
    </tr>
    
    <tr>
      <td>Node Address:</td>
      <td><?php
      
      echo "$addr ($suff)";
      
      ?></td>
    </tr>
    
  </tbody>
</table>

<?php

$nc_id_arr = $dbase->search_for_nodecouples( $id );
$conn_count = count($nc_id_arr);

?>
<h2>Segments</h2>
<p><?php echo "<strong>$ltitle</strong>" . " " . (($conn_count == 0) ? "is an <strong>orphan</strong> (click <a href=\"n_del.php?id=$id\">here</a> to remove it)" : "belongs to <strong>$conn_count</strong> " . (($conn_count == 1) ? "segment" : "segments")); ?>.</p>

<?php 

if ( count( $nc_id_arr ) ) {

?>
<table class="type2p" cellspacing="1">
  <col class="col2" /><col class="col2" /><col class="col2" /><col class="col1" />
  
  <thead>
    <tr>
      <th>Other Node Name</th>
      <th>Other Node Address</th>
      <th>Segment QoS Class</th>
      <th>Controls</th>
    </tr>
  </thead>
  
  <tbody>
    <?php      
      
      $has_nc_del = has_flag( $uid, "nc.del" );
      $has_n_view = has_flag( $uid, "n.view" );
      $has_bkt_view = has_flag( $uid, "bkt.view" );
      
      $nc_id_arr = $dbase->search_for_nodecouples( $id );
      
      foreach ( $nc_id_arr as $nc_id )
      {
        $nc = $dbase->get_nodecouple($nc_id);
        $categories = $dbase->get_thresholding_categories();
        $cat = $dbase->get_thresholding_category($nc_id);
        
        $children = $nc->get_nodes();
        $init = $children[ 0 ]; $fin = $children[ 1 ];
        
        if ( $init->get_id() == $id )
        {
          $n_id = $fin->get_id();
          $n_name = $fin->get_title();
          $n_addr = $fin->get_address();
        }
        else 
        {
          $n_id = $init->get_id();
          $n_name = $init->get_title();
          $n_addr = $init->get_address();
        }
        
        $categories[ $cat ] = "<strong>" . $categories[$cat] . "</strong>";
        
        foreach ( $categories as $c_id => $c_name )
        {
          if ( $c_id == $cat ) continue;
          $categories[$c_id] = "<a href=\"nc_chgcls.php?bkt=$c_id&nc=$nc_id\">$c_name</a>";
        }
        
        $title2 = implode(" / ", array_values($categories));
        
        $title1 = $has_n_view ? ui_create_link( $n_name, "n_view.php?id=$n_id" ) : $n_name;        
        
        $deletionLink = ($has_nc_del) ? ui_create_link( "Break link", "nc_del.php?id=$nc_id" ) : "Break link";
        
        echo "<tr>";
        echo "<td>$title1</td>";
        echo "<td>$n_addr</td>";
        echo "<td>&laquo; $title2 &raquo;</td>";
        echo "<td>$deletionLink</td>";
        echo "</tr>";
      }
      
    ?>
  </tbody>
</table>

<?php
}

include 'inc/footer.php';

?>